//
//  WBUserManager.m
//  WeatherBar
//
//  Created by Bert on 2024/6/7.
//

#import "WBUserManager.h"

#import "WBControllerManager.h"

@interface WBUserManager ()
@property (nonatomic ,copy) NSString *locationStatusKey;
@end

@implementation WBUserManager
+ (instancetype)shareManager
{
    static WBUserManager *manager;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        manager = [[super allocWithZone:NULL] init];
    });
    return manager;
}
+ (id)allocWithZone:(struct _NSZone *)zone {
    return [WBUserManager shareManager];
}

- (id)copyWithZone:(struct _NSZone *)zone {
    return [WBUserManager shareManager];
}

- (instancetype)init{
    if (self = [super init]) {
        
    }
    return self;
}

- (WBUserModel *)userModel {
    if (!_userModel) {
        NSDictionary *userDic = [WBDBManager getInDocumentWithObjectByFileName:NSStringFromClass(WBUserModel.class)];
        _userModel = [WBUserModel modelWithDictionary:userDic];
    }
    return _userModel;
}

/// 更新本地账户信息
- (void)updateUserInfo:(WBUserModel *)userModel
{
    NSDictionary *userDic = [userModel mj_keyValues];
    NSDictionary *getDic = [WBDBManager getInDocumentWithObjectByFileName:NSStringFromClass(userModel.class)];
    [WBDBManager removeInDocumentWithObjectByFileName:NSStringFromClass(userModel.class)];
    [WBDBManager saveInDocumentWithObject:userDic byFileName:NSStringFromClass(userModel.class)];
    [WBUserManager shareManager].userModel = userModel;
}
/// 用户上线
- (void)userLogin:(WBUserModel *)userModel {
    [self updateUserInfo:userModel];
}

/// 用户下线
- (void)userLogout {
    self.userModel = nil;
    [WBDBManager removeInDocumentWithObjectByFileName:NSStringFromClass([WBUserModel class])];
}

@end
